package nero.yncc.com.createart.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanArticleType;
import nero.yncc.com.createart.bean.BeanMessage;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.NoScrollViewPager;
import nero.yncc.com.createart.fragment.FragmentCategory;
import nero.yncc.com.createart.fragment.FragmentFound;
import nero.yncc.com.createart.fragment.FragmentIndex;
import nero.yncc.com.createart.fragment.FragmentShopcar;
import nero.yncc.com.createart.fragment.FragmentUcenter;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.UserDataUtil;

/**
 * Created by nero on 17-6-21.
 */

@ContentView(R.layout.activity_index)
public class ActivityIndex extends BaseActivity {
    @ViewInject(R.id.vp_index)
    NoScrollViewPager vp_index;

    @ViewInject(R.id.icon_index)
    IconView icon_index;
    @ViewInject(R.id.tv_index)
    TextView tv_index;
    @ViewInject(R.id.ll_index)
    LinearLayout ll_index;

    @ViewInject(R.id.icon_type)
    IconView icon_type;
    @ViewInject(R.id.tv_type)
    TextView tv_type;
    @ViewInject(R.id.ll_type)
    LinearLayout ll_type;

    @ViewInject(R.id.icon_found)
    IconView icon_found;
    @ViewInject(R.id.tv_found)
    TextView tv_found;
    @ViewInject(R.id.ll_found)
    LinearLayout ll_found;

    @ViewInject(R.id.icon_shopcar)
    IconView icon_shopcar;
    @ViewInject(R.id.tv_shopcar)
    TextView tv_shopcar;
    @ViewInject(R.id.ll_shopcar)
    LinearLayout ll_shopcar;

    @ViewInject(R.id.icon_mine)
    IconView icon_mine;
    @ViewInject(R.id.tv_mine)
    TextView tv_mine;
    @ViewInject(R.id.ll_mine)
    LinearLayout ll_mine;


    FragmentIndex fragmentIndex;
    FragmentCategory fragmentCategory;
    FragmentFound fragmentFound;
    FragmentShopcar fragmentShopcar;
    FragmentUcenter fragmentUcenter;

    @Override
    protected void initView() {
        super.initView();
        setDownColor(icon_index, tv_index);
        vp_index.setOffscreenPageLimit(5);
        setStateBarColor(R.color.translate);
    }

    @Override
    protected void initData() {
        super.initData();
        fragmentIndex = new FragmentIndex();
        fragmentCategory = new FragmentCategory();
        fragmentFound = new FragmentFound();
        fragmentShopcar = new FragmentShopcar();
        fragmentUcenter = new FragmentUcenter();

        vp_index.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return fragmentIndex;
                    case 1:
//                        return new FragmentIndex();
                        return fragmentCategory;
                    case 2:
                        return fragmentFound;
//                        return new FragmentIndex();
                    case 3:
//                        return new FragmentIndex();
                        return fragmentShopcar;
                    case 4:
                        return fragmentUcenter;
                }
                return null;
            }

            @Override
            public int getCount() {
                return 5;
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        //设置viewpager的更换事件
        vp_index.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setDownColor(icon_index, tv_index);
                        break;
                    case 1:
                        setDownColor(icon_type, tv_type);
                        break;
                    case 2:
                        setDownColor(icon_found, tv_found);
                        break;
                    case 3:
                        setDownColor(icon_shopcar, tv_shopcar);
                        break;
                    case 4:
                        setDownColor(icon_mine, tv_mine);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
//        初始化下方导航点击
        ll_index.setOnClickListener(this);
        ll_type.setOnClickListener(this);
        ll_found.setOnClickListener(this);
        ll_shopcar.setOnClickListener(this);
        ll_mine.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id) {
            //下面是下方导航条点击
            case R.id.ll_index:
                vp_index.setCurrentItem(0);
                break;
            case R.id.ll_type:
                vp_index.setCurrentItem(1);
                break;
            case R.id.ll_found:
                vp_index.setCurrentItem(2);
                break;
            case R.id.ll_shopcar:
                vp_index.setCurrentItem(3);
                break;
            case R.id.ll_mine:
                vp_index.setCurrentItem(4);
                break;

        }
    }

    /**
     * 设置下方导航条的颜色
     *
     * @param icon_now 当前选中的ICONVIEW
     * @param tv_now   当前选中的TEXTVIEW
     */
    private void setDownColor(IconView icon_now, TextView tv_now) {
        icon_index.setTextColor(getResources().getColor(R.color.textColorGray));
        icon_type.setTextColor(getResources().getColor(R.color.textColorGray));
        icon_found.setTextColor(getResources().getColor(R.color.textColorGray));
        icon_shopcar.setTextColor(getResources().getColor(R.color.textColorGray));
        icon_mine.setTextColor(getResources().getColor(R.color.textColorGray));

        tv_index.setTextColor(getResources().getColor(R.color.textColorGray));
        tv_type.setTextColor(getResources().getColor(R.color.textColorGray));
        tv_found.setTextColor(getResources().getColor(R.color.textColorGray));
        tv_shopcar.setTextColor(getResources().getColor(R.color.textColorGray));
        tv_mine.setTextColor(getResources().getColor(R.color.textColorGray));

        icon_now.setTextColor(getResources().getColor(R.color.project_yellow));
        tv_now.setTextColor(getResources().getColor(R.color.project_yellow));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (vp_index.getCurrentItem() == 3) {
                //如果正处于购物车界面 那么判断购物车是否处于编辑 是的话模拟点击还原先
                if (fragmentShopcar.isEdit()) {
                    return true;
                }
            }

            if (vp_index.getCurrentItem() != 0) {
                //如果不处于首页 那么返回首页pager
                vp_index.setCurrentItem(0);
                return true;
            }
        }


        return super.onKeyDown(keyCode, event);
    }

    HashMap<String,String> hashMap_type=new HashMap<>();
    @Override
    protected void getHttpData() {
        super.getHttpData();
        UserDataUtil.getArticleType(this);
    }
}
